package com.tmt.im.wallet.helper;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;

import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

/**
 * 分词器助手累
 * @Description TODO
 * @Author wuyi
 * @Date 2024/9/2 15:59
 * @Version 1.0
 **/
@Slf4j
public class IKAnalyzerHelper {

    public static List<String> iKSegToList(String content) {
        List<String> list = new ArrayList<>();
        try {
            if (!StringUtils.hasText(content)) {
                return list;
            }
            StringReader sr = new StringReader(content);
            // 关闭智能分词 (对分词的精度影响较大)
            IKSegmenter ik = new IKSegmenter(sr, false);
            Lexeme lex;
            while ((lex = ik.next()) != null) {
                String lexemeText = lex.getLexemeText();
                list.add(lexemeText);
            }
        } catch (Exception e) {
            log.error("分词异常");
        }
        return list;
    }
}
